﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WeiTap.Article.Dal.db;
using WeiTap.Article.Model;
using WeiTap.Data.Core;

namespace WeiTap.Article.Dal
{
    /// <summary>
    /// 文章标签
    /// </summary>
    public static class ArticelTagManager
    {
        /// <summary>
        /// 获取所有标签
        /// </summary>
        /// <returns></returns>
        public static async Task<IList<ArticelTag>> GetTags()
        {
            using (var db = new Sql003Database())
            {
                return await db.FetchAsync<ArticelTag>("select * from info_Dyb_Tags where isactive=1 ");
            }
        }
        /// <summary>
        /// 通过名称获取标签
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public static async Task<ArticelTag> GetTag(string name)
        {
            using (var db = new Sql003Database())
            {
                var sql=new Sql();
                sql.Select("*");
                sql.From("vinfo_Dyb_Tags");
                sql.Where("isactive=1 and name=@0", name);
                return await db.Single<ArticelTag>(sql);
            }
        }
        /// <summary>
        ///插入一条新的标签
        /// </summary>
        /// <param name="tag"></param>
        /// <returns></returns>
        public static async Task<int> Add(ArticelTag tag)
        {
            using (var db = new Sql003Database())
            {
                return (int)await db.InsertAsync(tag);
            }
        }
        /// <summary>
        /// 更新一条标签记录
        /// </summary>
        /// <param name="tag"></param>
        /// <returns></returns>
        public static async Task Update(ArticelTag tag)
        {
            using (var db = new Sql003Database())
            {
                await db.UpdateAsync(tag);
            }
        }
    }
}
